#include <QDesktopServices>
#include <QCompleter>
#include <QMessageBox>


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "collection.h"
#include "resultmodel.h"
#include "coverdelegate.h"
#include "videopage.h"
#include "diskscanner.h"
#include "settingsdialog.h"

MainWindow *MainWindow::m_mw;
QStringList MainWindow::m_log;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    m_mw = this;
    //qInstallMsgHandler(&showMessage); // This makes all qWarnings, qDebugs, etc show up in the status bar

    // Initialize the widgets
    ui->setupUi(this);

    // Set up the tagfilter
    ui->tagFilter->setCompleter(new QCompleter(Collection::listTags()));
    connect(ui->tagFilterButton, SIGNAL(clicked()), SLOT(addFilterTag()));
    connect(ui->tagFilter, SIGNAL(returnPressed()), SLOT(addFilterTag()));
    connect(ui->removeFilterButton, SIGNAL(clicked()), SLOT(removeFilterTags()));

    connect(ui->videoList, SIGNAL(clicked(QModelIndex)), SLOT(openVideo(QModelIndex)));
    connect(ui->videoScenes, SIGNAL(linkActivated(QString)), SLOT(launchFile(QString)));
    connect(ui->videoTags, SIGNAL(linkActivated(QString)), SLOT(addFilterTag(QString)));

    // Connect our actions
    connect(ui->actionQuit, SIGNAL(triggered()), SLOT(deleteLater()));
    connect(ui->actionScan, SIGNAL(triggered()), SLOT(launchScan()));
    connect(ui->actionSettings, SIGNAL(triggered()), SLOT(displaySettings()));

    // Set up the video list view
    ui->videoList->setModel(new ResultModel(QStringList()));
    ui->videoList->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->videoList->setItemDelegate(new CoverDelegate());
    ui->videoList->horizontalHeader()->setResizeMode(2, QHeaderView::Stretch); // let the tags expand
    ui->videoList->horizontalHeader()->hide();

    ui->videoGroup->hide();
    showMaximized();
}

MainWindow::~MainWindow()
{
    delete ui;
    Collection::close();
}

void MainWindow::addFilterTag(QString tag)
{
    QStringList validTags = Collection::listTags();

    if (tag.isEmpty())
        tag = ui->tagFilter->text();

    if (!validTags.contains(tag) || m_tagFilters.contains(tag)) {
        ui->tagFilter->clear();
        return;
    }

    m_tagFilters.append(tag);

    QAbstractItemModel *old = ui->videoList->model();
    ui->videoList->setModel(new ResultModel(m_tagFilters));
    delete old;

    ui->filterTagList->clear();
    ui->filterTagList->addItems(m_tagFilters);

    ui->tagFilter->clear();
}

void MainWindow::removeFilterTags()
{
    QList<QListWidgetItem*> filtersToRemove = ui->filterTagList->selectedItems();
    foreach (QListWidgetItem *item, filtersToRemove) {
        m_tagFilters.removeAll(item->text());
    }

    ui->filterTagList->clear();
    ui->filterTagList->addItems(m_tagFilters);
}


void MainWindow::openVideo(QModelIndex index)
{
    ui->videoGroup->show();
    Video video = reinterpret_cast<ResultModel*>(ui->videoList->model())->getVideo(index.row());

    ui->videoGroup->setTitle(video.name());

    ui->videoCover->setPixmap(QPixmap::fromImage(QImage(video.cover().toLocalFile())));

    QString videoTags = "Tags: <ul>";
    foreach(QString tag, video.tags()){
        videoTags += "<li><a href=\"" + tag + "\">" + tag + "</a></li>";
    }
    videoTags += "</ul><br/>";
    ui->videoTags->setText(videoTags);


    QString videoScenes = "Scenes: <ul>";
    foreach (QUrl url, video.files()){
        videoScenes += "<li>";
        videoScenes += "<a href='" + url.toString() + "'>";
        videoScenes += "<img width=100 src='" + Collection::getThumbnail(url.toString()).toString() + "'>";
        videoScenes += "</a></li>";
    }
    videoScenes += "</ul>";
    //ui->videoScenes->setTextFormat(Qt::RichText);
    ui->videoScenes->setText(videoScenes);
}

void MainWindow::launchFile(QString url)
{
     QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
}

void MainWindow::launchScan()
{
    DiskScanner *scanner = new DiskScanner();
    scanner->start();
}

void MainWindow::displaySettings()
{
    SettingsDialog *settings = new SettingsDialog();
    settings->show();
}
